﻿import QtQuick 2.0
import QtQuick.Window 2.12
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.12
import QtQuick.Controls.Material 2.0

import "../components"


Item {

    id: settingsDefault

    anchors.fill: parent

    property int tabPage: 0         // 0:样本默认 1:通道默认 2:指导建议默认
    property int comboPage: 0

    property var comboSample: ["样本名称", "样本类型", "扩增体系(μl)", "试剂盒", "耗材类型"]
    property var comboChannel: ["通道一", "通道二", "通道三", "通道四", "内标"]
    property var comboGuide: ["Guide1", "Guide2", "Guide3"]
    property var modelIsOperable: true

    onVisibleChanged: {
        if(visible){
            refresh(combox.getText())
        }
    }


    function refresh(type){
        // 刷新表格数据和combox数据
        var res = CSetUp.refTableData(type)
        if(type==="内标"){
            modelIsOperable = false
        }else{
            modelIsOperable = true
        }

        tableModel.clear()
        for(let i = 0; i < res.length; i ++){
            tableModel.append({
                                 "modelCode": res[i][0],
                                 "modelName": res[i][1],
                                 "modelIsDefault": res[i][2]
                              })

        }
        if(tableModel.count === 0){
            addButton.visible = true
        }else{
            addButton.visible = false
        }

    }
    function addItem(){
        CSetUp.addTableData(combox.getText(), "")
        comboGuide = CSetUp.tab3comread()
        refresh(combox.getText())

    }
    function delItem(code){
        if(tableModel.count === 1){
            mainwindow.showToast("至少保留一个元素", 1)
        }
        CSetUp.delTableData(combox.getText(), code)
        refresh(combox.getText())
    }
    function setDefault(code){
        CSetUp.setTableData(combox.getText(), code)
        refresh(combox.getText())
    }
    function updateItem(code, text){
        CSetUp.updataTableData(combox.getText(), text, code)
    }

    onTabPageChanged: {
        comboGuide = CSetUp.tab3comread()
        combox.changeCurrentIndex(0)
        if(tabPage === 0){
            combox.model = comboSample
        }else if(tabPage === 1){
            combox.model = comboChannel
        }else{
            combox.model = comboGuide
        }
        refresh(combox.getText())
    }

    // 样式
    Component.onCompleted: {
        // 初始化按钮样式
        var list = layout_tab.children
        for(let i = 0; i < list.length; i ++){
            list[i].button_color = "transparent"
            list[i].text_color = "#666666"
            list[i].button_color_select = "white"
            list[i].text_color_select = "#1677FF"
        }
        combox.model = comboSample
    }
    Rectangle{              // Tab ==============================================================

        height: 60*dp
        width: parent.width
        color: "#EFEFF2"
        Row{
            id: layout_tab
            anchors.fill: parent
            PushButton{
                height: 60*dp
                width: 120*dp
                title: "样本默认"
                isSelected: tabPage === 0
                onReleased: {
                    tabPage = 0
                }
            }PushButton{
                height: 60*dp
                width: 120*dp
                title: "通道默认"
                isSelected: tabPage === 1
                onReleased: {
                    tabPage = 1
                }
            }PushButton{
                height: 60*dp
                width: 150*dp
                title: "指导建议默认"
                isSelected: tabPage === 2
                onReleased: {
                    tabPage = 2
                }
            }
        }
    }

    CustomComboBox{         // Combox ==========================================================
        x: 20*dp
        y: 80*dp
        id: combox
        showListMaxCountinPage: 5
        model: ["样本名称", "样本类型", "扩增体系(μl)", "试剂盒", "耗材类型"]
        onCurrentText: {
            refresh(combox.getText())
        }
    }


    DefaultHead{            // Table ============================================================
        x: 20*dp
        y: 140*dp
        width: parent.width - 40*dp
    }    
    ListView{
        x: 20*dp
        y: 200*dp
        clip: true
        height: parent.height - 220*dp
        width: parent.width - 40*dp
        model: tableModel
        delegate: DefaultLine{
            title: modelName
            code: modelCode
            isDefault: modelIsDefault
            isOperable: modelIsOperable
        }
        ScrollBar.vertical: ScrollBar{}
    }
    ListModel{
        id: tableModel
    }

    PushButton{         // 防止把元素都删没了
        id: addButton
        y: 220*dp
        x: parent.width - 140*dp
        title: "添加"
        button_border: "#DDDDDD"
        visible: false
        onReleased: {
            addItem()
        }
    }
}
